package ICM.client;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import common.CommandMessage;
import common.Request;
import common.TextMessage;
import javax.swing.JComboBox;

public class ChairmanUI extends JPanel implements ActionListener,Observer {

	private Client c;
	private ClientUI ui;
	private String IDR;
	private JComboBox comboBox;
	private JTextField textidRequest;
	private JTextField textDateSend;
	private JTextField textDepartment;
	private SystemEngineerUI SE;
	private Request request;
	private JTextPane textComments;
	private JTextPane textReason;
	private JTextPane textDescription;
	private JTextField textDetermination;
	private JButton btnSaveChairmanReport;
	private JButton btnBack;
	private boolean Result1 = false;
	private boolean Result2 = false;

	/**
	 * Constructor - Build Assessor form
	 * Create the panel.
	 */
	public ChairmanUI(final Client c,final ClientUI ui) {
		
		this.c=c;
		this.ui=ui;
		setLayout(null);
		c.addObserver(this);
		
		CommandMessage msg11 = new CommandMessage("Get request data",new Integer[] {Integer.parseInt(SE.info[0])});
		try {
			c.sendToServer(msg11);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
		    Thread.sleep(200);
		} catch(InterruptedException ex) {
		    Thread.currentThread().interrupt();
		}
		
		btnSaveChairmanReport = new JButton("Save Chairman Report ");
		btnSaveChairmanReport.setBounds(600, 471, 180, 54);
		btnSaveChairmanReport.addActionListener(this);
		add(btnSaveChairmanReport);
		
		
		
		JLabel lblMeaningfulAssessment = new JLabel("Review and decision");
		lblMeaningfulAssessment.setFont(new Font("Tahoma", Font.PLAIN, 20));
		lblMeaningfulAssessment.setBounds(276, 11, 264, 35);
		add(lblMeaningfulAssessment);
				
		JLabel lblDetermination = new JLabel("Determination of the stage");
		lblDetermination.setBounds(24, 315, 150, 14);
		add(lblDetermination);
		
		JLabel lblIdrequest = new JLabel("idRequest");
		lblIdrequest.setBounds(26, 77, 63, 14);
		add(lblIdrequest);
		
		JLabel lblDescription = new JLabel("Description");
		lblDescription.setBounds(26, 102, 98, 14);
		add(lblDescription);
		
		JLabel lblReasonOfRequest = new JLabel("Reason of request");
		lblReasonOfRequest.setBounds(485, 102, 108, 14);
		add(lblReasonOfRequest);
		
		JLabel lblComments = new JLabel("Comments");
		lblComments.setBounds(26, 216, 98, 14);
		add(lblComments);
		
		JLabel lblDateOfSend = new JLabel("Date of send request");
		lblDateOfSend.setBounds(254, 77, 124, 14);
		add(lblDateOfSend);
		
		textidRequest = new JTextField();
		textidRequest.setBounds(107, 74, 86, 20);
		add(textidRequest);
		textidRequest.setColumns(10);
		textidRequest.setText(SE.info[0]);
		
		textDateSend = new JTextField();
		textDateSend.setBounds(376, 74, 86, 20);
		add(textDateSend);
		textDateSend.setColumns(10);
		
		String s1;
		Date d1;
		d1=request.getdateRequest();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
		s1=d1.toString();
		textDateSend.setText(s1);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(170, 210, 264, 84);
		add(scrollPane_2);
		
		textComments = new JTextPane();
		scrollPane_2.setViewportView(textComments);
		textComments.setText(request.getComments());
		
		JLabel lblDepartment = new JLabel("Department");
		lblDepartment.setBounds(527, 77, 89, 14);
		add(lblDepartment);
		
		textDepartment = new JTextField();
		textDepartment.setBounds(600, 74, 158, 20);
		add(textDepartment);
		textDepartment.setColumns(10);
		textDepartment.setText(request.getDepartment());
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(606, 96, 221, 109);
		add(scrollPane_1);
		
		textReason = new JTextPane();
		scrollPane_1.setViewportView(textReason);
		textReason.setText(request.getReasons());
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(170, 96, 264, 109);
		add(scrollPane);
		
		textDescription = new JTextPane();
		scrollPane.setViewportView(textDescription);
		textDescription.setText(request.getDescription());		
		
		textDetermination = new JTextField();
		textDetermination.setBounds(232, 312, 202, 20);
		add(textDetermination);
		textDetermination.setColumns(10);
		
		JLabel lblResult = new JLabel("Result");
		lblResult.setBounds(477, 315, 46, 14);
		add(lblResult);
		
		
	
		
		btnBack = new JButton("Back");
		btnBack.setBounds(120, 477, 124, 43);
		btnBack.addActionListener(this);
		add(btnBack);
		
		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"","Request another information","Commit","Reject"}));
		comboBox.addActionListener(this);
		comboBox.setBounds(621, 312, 195, 20);
		add(comboBox);
		

	}
	/**
	 * Server message received event handler
	 */
	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		String msg = "";
		String cmd = "";
		if (arg1 instanceof TextMessage)
			msg = ((TextMessage)arg1).ToDebugMessage();
		else if (arg1 instanceof CommandMessage) {
			CommandMessage ms=(CommandMessage)arg1;
			if(ms.getCommand().equals("request data"))
					request = (Request) ms.getParams()[1];
			if(ms.getCommand().equals("back")){
				Result1 = Boolean.parseBoolean(ms.getParams()[1].toString());
			}
			if(ms.getCommand().equals("Commit")){
				Result2 = Boolean.parseBoolean(ms.getParams()[1].toString());
			}
		}
		else
			msg = arg1.toString();
	}

	/**
	 * Button click event handler
	 */
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		CommandMessage msg = null;
		if(e.getSource().equals(btnSaveChairmanReport)){
			try{
				
					if (comboBox.getSelectedItem().toString().equals("Request another information")){
						msg = new CommandMessage("Review and decision to last stage",new Integer[] {Integer.parseInt(SE.info[0])});
						try {
							c.sendToServer(msg);
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						try {
						    Thread.sleep(200);
						} catch(InterruptedException ex) {
						    Thread.currentThread().interrupt();
						}
					}
					else if (comboBox.getSelectedItem().toString().equals("Commit")){
						msg = new CommandMessage("Commit decision stage",new Integer[] {Integer.parseInt(SE.info[0])});
						try {
							c.sendToServer(msg);
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						try {
						    Thread.sleep(200);
						} catch(InterruptedException ex) {
						    Thread.currentThread().interrupt();
						}
					}
					else if (comboBox.getSelectedItem().toString().equals("Reject")){
						msg = new CommandMessage("Reject decision stage",new Integer[] {Integer.parseInt(SE.info[0])});
						try {
							c.sendToServer(msg);
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						try {
						    Thread.sleep(200);
						} catch(InterruptedException ex) {
						    Thread.currentThread().interrupt();
						}
					}
					
						
					else
						ui.ShowMessageBox("Error", "" ,JOptionPane.ERROR_MESSAGE);
					ui.SetContent(ui.Main_Screen);
				
				}
			catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
		};
		
		if(e.getSource().equals(btnBack)){
			try 
			{ui.SetContent(ClientUI.SystemEngineerWindow_Screen);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
			
		}
		
	}
	}
